package com.iplant.wms.controller.wpo;

import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.annotation.trace.TraceModuleAPI;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.WPOService;
import com.iplant.wms.service.po.wpo.WMSPurchaseOrderItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 采购订单
 *
 * @author YouWang·Peng
 * @CreateTime 2022-4-5 11:43:51
 */
@TraceModuleAPI("采购订单")
@Slf4j
@RestController
@RequestMapping("/api/WMSPurchaseOrderItem")
public class WMSPurchaseOrderItemController extends BasicController<WMSPurchaseOrderItem> {


    @Autowired
    WPOService wWPOService;

    public WMSPurchaseOrderItemController() {
        super(WMSPurchaseOrderItem.class);
    }

    /**
     * 保存子项
     */
    @PostMapping("/UpdateList")
    public Object UpdateList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);
            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            int wNotThrown=wParam.containsKey("NotThrown")?StringUtils.parseInt(wParam.get("NotThrown")):0;
            // 获取参数
            List<WMSPurchaseOrderItem> wWMSPurchaseOrderItemList = CloneTool.CloneArray(wParam.get("data"),
                                                                                        WMSPurchaseOrderItem.class);

            if (wWMSPurchaseOrderItemList == null || wWMSPurchaseOrderItemList.size() == 0
                    || wWMSPurchaseOrderItemList.stream()
                    .anyMatch(p -> p.PurchaseOrderID <= 0 || StringUtils.isEmpty(p.PurchaseOrderCode))) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wWPOService.WMS_UpdatePurchaseOrderItemList(wLoginUser,
                                                                                                wWMSPurchaseOrderItemList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)||wNotThrown==1) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wWMSPurchaseOrderItemList, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }









}
